<template>
  <div>
    <el-row>
      <el-col :span="6">
        <el-button
          type="primary"
          plain
          size="small"
          class="btnNew"
          @click="addNewPer"
          v-no-more-click
          >新增权限</el-button
        >
        <div class="left">
          <el-tree
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
      </el-col>
      <el-col :span="14"
        ><div>
          <el-form
            label-width="80px"
            ref="form"
            :model="form"
            :rules="rules"
            status-icon
          >
            <el-row>
              <el-col :span="12">
                <el-form-item class="treeItem" label="上级权限" prop="parentId">
                  <treeselect
                    class="treeSelect"
                    :options="deptData"
                    :clearable="false"
                    v-model="form.parentId"
                    :normalizer="normalizer"
                    :placeholder="placeholder"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="权限名称" prop="name">
                  <el-input v-model="form.name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" v-if="form.type == 2">
                <el-form-item label="权限编码" prop="perms">
                  <el-input
                    v-model="form.perms"
                    v-bind:disabled="showDisabled"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="权限类型" prop="type">
                  <el-select v-model="form.type" placeholder="请选择">
                    <el-option
                      v-for="item in permTypeData"
                      :key="item.id"
                      :label="item.itemText"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12" v-if="form.type == 1 || form.type == 2">
                <el-form-item label="访问路径" prop="url" v-if="isShow">
                  <el-input v-model="form.url"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="排序值" prop="sort">
                  <el-input-number
                    v-model="form.sort"
                    :min="1"
                    :max="10"
                    label="描述文字"
                  ></el-input-number>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="down" v-no-more-click>取 消</el-button>
            <el-button
              type="primary"
              @click="submit"
              v-if="permsSaveBtn || permsUpdateBtn"
              v-no-more-click
              >提交</el-button
            >
            <p
              style="margin-top: 10px"
              v-show="this.form.id != null"
              v-if="permsDeleteBtn"
            >
              如该权限无效，您可以
              <el-popconfirm
                title="确定删除此数权限？"
                @onConfirm="btnDelette"
                v-if="permsDeleteBtn"
              >
                <el-button
                  slot="reference"
                  type="text"
                  size="medium"
                  v-if="permsDeleteBtn"
                  v-no-more-click
                  >删除此权限</el-button
                >
              </el-popconfirm>
            </p>
          </span>
        </div></el-col
      >
    </el-row>
  </div>
</template>

<script>
import {
  getPermType,
  getMenuList,
  getPermFindone,
  savePerms,
  updatePerms,
  delPerms
} from "../../../src/assets/js/api";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { checkPermiss } from "../../assets/js/utlis";
export default {
  name: "RolePermissions",
  props: ["dialog"],
  components: {
    Treeselect
  },
  data() {
    return {
      normalizer(node) {
        //去掉children=[]的children属性
        if (node.children == null) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.label,
          children: node.children
        };
      },
      deptData: [],
      placeholder: "请选择权限",
      form: {
        sort: 1
      },
      data: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      showDisabled:false,
      treeId: "",
      //    权限类型
      permTypeData: [],
      showUrl: false,
      rules: {
        parentId: [
          { required: true, message: "请选择上级权限", trigger: "change" }
        ],
        name: [
          { required: true, message: "请输入权限名称", trigger: "change" }
        ],
        perms: [
          { required: true, message: "请输入权限编码", trigger: "change" }
        ],
        type: [{ required: true, message: "请选择权限类型", trigger: "change" }]
      }
    };
  },
  created() {
    this.golbalPermissions = localStorage.getItem("permissions").split(",");
    this.getMenuList();
    this.getPermType();
  },
  computed: {
    // 添加权限
    permsSaveBtn: function() {
      return checkPermiss("sys:perms:save", this.golbalPermissions);
    },
    // 修改权限
    permsUpdateBtn: function() {
      return checkPermiss("sys:perms:update", this.golbalPermissions);
    },
    // 删除权限
    permsDeleteBtn: function() {
      return checkPermiss("sys:perms:delete", this.golbalPermissions);
    },
    // showDisabled: function() {
    //   if (this.form.perms) {
    //     return true;
    //   } else {
    //     return false;
    //   }
    // },
    isShow: function() {
      if (this.form.type == null) {
        return false;
      } else {
        return true;
      }
    }
  },
  methods: {
    //获取权限菜单
    getMenuList() {
      getMenuList().then(res => {
        this.data = res.data;
        let arr = [
          { pid: null, id: 0, children: null, label: "此作为一级权限" }
        ];
        this.deptData = arr.concat(res.data);
      });
    },
    // 关闭操作
    down() {
      this.$emit("dialog", false);
    },
    // 提交操作
    submit() {
      this.$refs.form.validate().then(() => {
        if (this.form.id == null) {
          if (this.permsSaveBtn == false) {
            this.$message({
              message: "暂无该功能权限",
              type: "warning"
            });
            return;
          }
          savePerms(this.form).then(res => {
            this.$message({
              message: res.message,
              type: "success"
            });
            this.getMenuList();
          });
        } else {
          if (this.permsUpdateBtn == false) {
            this.$message({
              message: "暂无该功能权限",
              type: "warning"
            });
            return;
          }
          if (this.form.parentId == this.treeId) {
            this.$message({
              message: "请选择正确的上级权限",
              type: "warning"
            });
            return;
          }
          updatePerms(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                message: res.message,
                type: "success"
              });
            } else {
              this.$message({
                message: res.message,
                type: "error"
              });
            }
            this.getMenuList();
          });
        }
      });
    },
    //删除权限
    btnDelette() {
      delPerms(this.form.id).then(res => {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getMenuList();
        this.$refs.form.resetFields();
      });
    },
    //获取权限类型
    getPermType() {
      getPermType("perm_type").then(res => {
        let arr = res.data;
        let arr2 = arr.map(function(arr) {
          arr.id = parseInt(arr.itemValue);
          return arr;
        });
        this.permTypeData = arr2;
      });
    },
    //点击树形获取值
    handleNodeClick(data) {
      this.$emit("RoleTitle", "编辑权限");
      this.getPermFindone(data.id);
      this.treeId = data.id;
      this.showDisabled = true;
    },
    //根据权限id查询一条详细信息
    getPermFindone(id) {
      getPermFindone(id).then(res => {
        this.form = res.data;
      });
    },
    //新增权限按钮
    addNewPer() {
      this.$emit("RoleTitle", "新增权限");
      this.form = {};
      this.form.sort = 1;
      // this.showDisabled = true;
    }
  }
};
</script>

<style scoped type="less">
.left {
  width: 200px;
  height: 300px;
  border: 1px black solid;
  overflow: auto;
}
.btnNew {
  position: absolute;
  top: -36px;
  left: 0;
}
::v-deep .el-col-14 {
  margin-left: 40px;
}
::v-deep .el-input-number {
  width: 160px;
  line-height: 28px;
}
::v-deep .el-input__inner {
  height: 30px;
}
.btnDelette {
  color: rgb(28, 158, 254);
  cursor: pointer;
}
.btnDelette:hover {
  color: red;
}
.treeSelect {
  height: 30px;
  margin-top: 5px;
}
.treeSelect ::v-deep.vue-treeselect__control {
  line-height: 28px;
  height: 30px;
}
.treeSelect ::v-deep.vue-treeselect__single-value,
.treeSelect ::v-deep.vue-treeselect__placeholder {
  line-height: 28px;
}

::v-deep .el-col-12 {
  height: 60px;
}
</style>
